package com.smartalarmclock.UI;

import com.smartalarmclock.Logic.AcSyncService;

import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;

public class MainPagerActivity extends FragmentActivity 
	implements 	ProfileFragment.OnFragmentInteractionListener,
				HistoryFragment.OnFragmentInteractionListener,
				GroupFragment.OnFragmentInteractionListener,
				ScheduleFragment.OnFragmentInteractionListener
{
	private ViewPager m_Pager;
	private PagerAdapter m_PagerAdapter;
	private MainPagerActivity m_Activity = this;
	private ImageView m_ProfileButton;
	private ImageView m_VIPButton;
	private ImageView m_HistoryButton;
	private ImageView m_GroupButton;
	private ImageView m_ScheduleButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_main_pager);
				
		Intent acSyncService = new Intent(MainPagerActivity.this, AcSyncService.class);
        startService(acSyncService);
		
		m_ProfileButton = (ImageView)findViewById(R.id.image_pager_profile);
		m_VIPButton = (ImageView)findViewById(R.id.image_pager_VIP);
		m_HistoryButton = (ImageView)findViewById(R.id.image_pager_history);
		m_GroupButton = (ImageView)findViewById(R.id.image_pager_group);
		m_ScheduleButton = (ImageView)findViewById(R.id.image_pager_schedule);
		
		// Instantiate a ViewPager and a PagerAdapter.
        m_Pager = (ViewPager) findViewById(R.id.pager);
        m_PagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        m_Pager.setAdapter(m_PagerAdapter);
        m_Pager.setOnPageChangeListener(pageChangeListener);
        
        m_ProfileButton.setBackgroundColor(getResources().getColor(R.color.nvbar_img_bgcolor));
        m_ProfileButton.setOnClickListener(profileButtonOnClickListener);
		m_VIPButton.setBackgroundColor(Color.TRANSPARENT);
		m_VIPButton.setOnClickListener(vipButtonOnClickListener);
		m_HistoryButton.setBackgroundColor(Color.TRANSPARENT);
		m_HistoryButton.setOnClickListener(historyButtonOnClickListener);
		m_GroupButton.setBackgroundColor(Color.TRANSPARENT);
		m_GroupButton.setOnClickListener(groupButtonOnClickListener);
		m_ScheduleButton.setBackgroundColor(Color.TRANSPARENT);
		m_ScheduleButton.setOnClickListener(scheduleButtonOnClickListener);
		
		m_Pager.setCurrentItem(0);
	}
	
	private OnClickListener profileButtonOnClickListener = new OnClickListener()
	{	
		@Override
		public void onClick(View v) {
			m_Pager.setCurrentItem(0);
		}
	};
	
	private OnClickListener vipButtonOnClickListener = new OnClickListener()
	{	
		@Override
		public void onClick(View v) {
			m_Pager.setCurrentItem(1);
		}
	};
	
	private OnClickListener historyButtonOnClickListener = new OnClickListener()
	{	
		@Override
		public void onClick(View v) {
			m_Pager.setCurrentItem(2);
		}
	};
	
	private OnClickListener groupButtonOnClickListener = new OnClickListener()
	{	
		@Override
		public void onClick(View v) {
			m_Pager.setCurrentItem(3);
		}
	};
	
	private OnClickListener scheduleButtonOnClickListener = new OnClickListener()
	{	
		@Override
		public void onClick(View v) {
			m_Pager.setCurrentItem(4);
		}
	};

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener()
	{
		@Override
		public void onPageSelected(int position) {
			m_ProfileButton.setBackgroundColor(Color.TRANSPARENT);
			m_VIPButton.setBackgroundColor(Color.TRANSPARENT);
			m_HistoryButton.setBackgroundColor(Color.TRANSPARENT);
			m_GroupButton.setBackgroundColor(Color.TRANSPARENT);
			m_ScheduleButton.setBackgroundColor(Color.TRANSPARENT);
			
			switch (position)
			{
			case 0:
				m_Activity.setTitle("Profiles");
				m_ProfileButton.setBackgroundColor(getResources().getColor(R.color.nvbar_img_bgcolor));
				break;
			case 1:
				m_Activity.setTitle("VIP");
				m_VIPButton.setBackgroundColor(getResources().getColor(R.color.nvbar_img_bgcolor));
				break;
			case 2:
				m_Activity.setTitle("History");
				m_HistoryButton.setBackgroundColor(getResources().getColor(R.color.nvbar_img_bgcolor));
				break;
			case 3:
				m_Activity.setTitle("Group");
				m_GroupButton.setBackgroundColor(getResources().getColor(R.color.nvbar_img_bgcolor));
				break;
			case 4:
				m_Activity.setTitle("Schedule");
				m_ScheduleButton.setBackgroundColor(getResources().getColor(R.color.nvbar_img_bgcolor));
				break;
			}
		}
		
		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			
		}
		
		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub
			
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_pager, menu);
		return true;
	}
	
	@Override
    public void onBackPressed() {
        if (m_Pager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            m_Pager.setCurrentItem(m_Pager.getCurrentItem() - 1);
        }
    }

    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
        
        @Override
        public Fragment getItem(int position) {
        	switch (position)
        	{
        	case 0:
        		return new ProfileFragment();
        	case 1:
        		return new VIPFragment();
        	case 2:
        		return new HistoryFragment();
        	case 3:
        		return new GroupFragment();
        	case 4:
        		return new ScheduleFragment();
        	}
        	
        	return new ProfileFragment();
        }

        @Override
        public int getCount() {
            return 5;
        }
    }

	@Override
	public void onFragmentInteraction(Uri uri) {
		// TODO Auto-generated method stub
		
	}
}

